<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button class="btn_get">GET请求</button>
  <button class="btn_post">POST请求</button>
  <button class="btn_get2">GET请求的完整写法</button>
  <button class="btn_post2">POST请求的完整写法</button>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script>
    document.querySelector('.btn_get').addEventListener('click', () => {
      axios.get('http://localhost:3000/api/getmsg').then(result => {
        console.log(result);
        console.log(result.data);
      }).catch(error => {
        console.log('错误');
      }).finally(() => {
        console.log('最终都会执行的');
      })
    })
    document.querySelector('.btn_post').addEventListener('click', () => {
      axios.post('http://localhost:3000/api/postmsg', {
        name: '张三',
        age: 12
      }).then(result => {
        console.log(result);
        console.log(result.data);
      }).catch(error => {
        console.log('错误');
      }).finally(() => {
        console.log('最终都会执行的');
      })
    })
    document.querySelector('.btn_get2').addEventListener('click', () => {
      axios({
        url: 'promisedata1',
        baseURL: 'http://localhost:3000/api/',
        method: 'GET',
        params: {
          'name': 'student',
          'age': 12
        }
      }).then(result => {
        console.log(result);
        console.log(result.data);
      }).catch(error => {
        console.log('错误');
      }).finally(() => {
        console.log('最终都会执行的');
      })
    })
    document.querySelector('.btn_post2').addEventListener('click', () => {
      axios({
        url: 'postmsg',
        baseURL: 'http://localhost:3000/api/',
        method: 'POST',
        data: {
          name: 'student',
          age: 12
        }
      }).then(result => {
        console.log(result);
        console.log(result.data);
      }).catch(error => {
        console.log('错误');
      }).finally(() => {
        console.log('最终都会执行的');
      })
    })
  </script>
</body>

</html>